using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using UnityGameFramework.Editor.ResourceTools;

namespace LS.Editor.Build
{
    public class BuilderResultConfig : ScriptableObject
    {
        private static readonly string ConfigFileName = "BuilderResultConfig";
        private static readonly string ConfigFilePath = $"ProjectSettings/{ConfigFileName}.asset";

        private static BuilderResultConfig s_Instance = null;

        /// <summary>
        /// 配置文件实例
        /// </summary>
        public static BuilderResultConfig Instance
        {
            get
            {
                if (s_Instance == null)
                {
                    s_Instance = CreateOrLoadInstance();
                }
                return s_Instance;
            }
        }

        /// <summary>
        /// 创建或者加载配置文件实例
        /// </summary>
        /// <returns></returns>
        public static BuilderResultConfig CreateOrLoadInstance()
        {
            BuilderResultConfig config = null;
            if (File.Exists(ConfigFilePath))
            {
                config = (BuilderResultConfig)(InternalEditorUtility.LoadSerializedFileAndForget(ConfigFilePath).SingleOrDefault());
                return config;
            }
            else
            {
                config = ScriptableObject.CreateInstance<BuilderResultConfig>();
                InternalEditorUtility.SaveToSerializedFileAndForget(new[] { config }, ConfigFilePath, true);
                return config;
            }
        }

        #region BuildResult

        /// <summary>
        /// 资源构建结果
        /// </summary>
        [System.Serializable]
        public class Result
        {
            public Platform buildPlatform;
            public string workingPath;
            public string outputPackagePath;
            public string outputFullPath;
            public string outputPackedPath;
        }

        public List<Result> lastBuildResults = new List<Result>();

        public void Save()
        {
            InternalEditorUtility.SaveToSerializedFileAndForget(new[] { this }, ConfigFilePath, true);
        }

        public void Refresh()
        {
            foreach (var item in lastBuildResults.ToArray())
            {
                bool existWorkingPath = !string.IsNullOrEmpty(item.workingPath) && Directory.Exists(item.workingPath);
                bool existPackagePath = !string.IsNullOrEmpty(item.outputPackagePath) && Directory.Exists(item.outputPackagePath);
                bool existFullPath = !string.IsNullOrEmpty(item.outputFullPath) && Directory.Exists(item.outputFullPath);
                bool existPackedPath = !string.IsNullOrEmpty(item.outputPackedPath) && Directory.Exists(item.outputPackedPath);
                bool existBuildResult = existWorkingPath || existPackagePath || existFullPath || existPackedPath;

                if (!existBuildResult)
                {
                    lastBuildResults.Remove(item);
                }
            }
        }

        public void SetResult(Platform platform,string workingPath,string outputPackagePath = null, string outputFullPath = null, string outputPackedPath = null)
        {
            Result result = lastBuildResults.Find(r => r.buildPlatform == platform);
            if(result == null)
            {
                result = new Result();
                lastBuildResults.Add(result);
            }
            result.buildPlatform = platform;
            result.workingPath = workingPath;
            result.outputPackagePath = outputPackagePath;
            result.outputFullPath = outputFullPath;
            result.outputPackedPath = outputPackedPath;
        }

        public Result GetResult(Platform platform)
        {
            return lastBuildResults.Find(r => r.buildPlatform == platform);
        }

        public bool HasResult(Platform platform)
        {
            return GetResult(platform) != null;
        }

        #endregion

    }
}
